% this file is almost identical to SalLocalInhibit.m in HRL ref code
function norm_out = maxnorm_sim(img_in)

% the normalization code (from HRL) ignores border pixels, but the hardware
    % considers border pixels, so pad the array before calling HRL code 
    img = padarray(img_in, [1,1]);

    global_max = max(img(:));
    if(global_max==0)
        global_max=1;
    end
    %img = img ./ maxVal;
    local_max_avg = LocalMaxStats(img);
    norm = (global_max - local_max_avg);
    norm_factor = (norm*norm)/(global_max^3);%(global_max); %(global_max^3); %norm factor = (M-u)^2/M
    
    %% printinf norm factor if > 7 - detect overflow from 4.20
%     if(norm_factor > 7)
%         disp(sprintf('norm factor = %E', norm_factor));
%     end
    
    %% Normalize the image with the norm factor
    norm_out_tmp = norm_factor*img;
    
    % remove the padding
    norm_out = norm_out_tmp(2:(end-1),2:(end-1));
    
end

